-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:
-- @email:
-- @date:     2024/10/18
-- @module:   state_manager_base
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class state_manager_base
local state_manager_base = {}

state_manager_base.curStateName = nil
state_manager_base.curState = nil
state_manager_base.stateDic = {}

--- 添加状态
function state_manager_base:add_state(name, state)
    if self.stateDic[name] then
        print(string.format("添加状态重复 【%s】", name))
        return
    end
    self.stateDic[name] = state
end

--- 更新状态
function state_manager_base:update_state()
    if self.curState then
        if not self:is_curState_leaving() then
            self.curState:on_update()
        end
        if self:is_curState_leaving() then
            local ret = self.curState:on_leaving()
            if ret then
                self.curState:on_leave()
                self:clear_curState()
                self:change_state(ret)
            end
        end
    end
end

--- 清理数据
function state_manager_base:clear_data()
    for k, v in pairs(self.stateDic) do
        v:clear_data()
    end
end

-- 清除当前状态
function state_manager_base:clear_curState()
    self.curState = nil
    self.curStateName = nil
end

-- 当前状态是否离开中
function state_manager_base:is_curState_leaving()
    return self.curState:is_leaving()
end

-- 当前状态是否暂停
function state_manager_base:is_curState_stop()
    return self.curState:is_stop()
end

-- 当前状态是否需要首次执行
function state_manager_base:curState_needFirstExecute()
    return self.curState:need_firstExecute()
end

function state_manager_base:curState_cancel_firstExecute()
    self.curState:cancel_firstExecute()
end

-- 设置当前状态离开
function state_manager_base:set_curState_leaving()
    self.curState:set_leaving(true)
end

-- 设置当前状态中断
function state_manager_base:set_curState_interrupt()
    self.curState:interrupt()
end

-- 移除主目标时调用
function state_manager_base:on_remove_mainTarget(stateName)
    local tmpState = self:get_state_by_name(stateName)
    if tmpState then
        tmpState:on_remove()
    end
end

-- 得到当前状态完成前的运行时间/毫秒
function state_manager_base:get_curState_runTime()
    return self.curState:get_run_time()
end

-- 得到当前状态总运行时间/毫秒
function state_manager_base:get_curState_totalRunTime()
    return self.curState:get_total_run_time()
end

-- 切换状态
function state_manager_base:change_state(name, isStopCurState)
    if self.curStateName == name then
        return
    end
    local tmpState = self:get_state_by_name(name)
    if not tmpState then
        print(string.format('没有对应状态 %s', name))
        return
    end
    if not self.curState then
        self.curStateName = tmpState.name
        self.curState = tmpState
        self.curState:on_enter()
    else
        self:set_curState_leaving()
        self.curState:set_isInterrupt(not isStopCurState)
    end
end

-- 获取当前状态
function state_manager_base:get_cur_state()
    return self.curState
end

-- 获取状态
function state_manager_base:get_state_by_name(name)
    return self.stateDic[name]
end

function state_manager_base:new(args)
    local ret = {}
    self.__index = self
    setmetatable(ret, self)
    return ret
end

return state_manager_base